﻿using System;
using System.IO;
using System.Text;

namespace Platform.DB
{
    public static class DBLogManager
    {
        #region 字段属性

        private const string fileName = "MYSQLLog";
        private static readonly object singleLock = new object();

        #endregion

        #region 公用方法

        public static void WriteDBLogToTxt(string strLog)
        {
            lock (singleLock)
            {
                String System_Now_Time = null;
                String File_Create_Time = null;
                System_Now_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"); //"HH:mm:ss
                File_Create_Time = DateTime.Now.ToString("yyyyMMdd");
                string logDocument = File_Create_Time + ".txt";

                string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                string logFilePath = str.Substring(0, str.LastIndexOf("\\")) + "\\" + fileName;
                if (!Directory.Exists(logFilePath))
                {
                    Directory.CreateDirectory(logFilePath);
                }
                logFilePath = logFilePath + "//" + logDocument;
                FileStream fs = new FileStream(logFilePath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(System_Now_Time + ":   " + strLog);
                sw.Flush();
                sw.Close();
                fs.Close();
            }
        }

        #endregion

    }
}
